package com.xiaoguang.selecttextview

import android.app.Dialog
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.text.Spannable
import android.text.Spanned
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.xiaoguang.selecttext.ItemClickListener
import com.xiaoguang.selecttext.SelectBuilder
import com.xiaoguang.selecttext.OnSelectListenerImpl
import com.xiaoguang.selecttext.SelectHelper
import com.zzhoujay.richtext.RichText

/**
 * 选择文字
 * hxg 2021/9/14 qq:929842234z
 */
class MsgDetailDialog(context: Context?, private val mText: CharSequence) : Dialog(
    context!!, R.style.SelectTextFragment
) {
    private lateinit var textView:TextView
    private var selectText: CharSequence? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setCancelable(true)
        setCanceledOnTouchOutside(true)
        setContentView(R.layout.dialog_select_text)

        // 一定要在setContentView之后调用，否则无效
        window!!.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        textView = findViewById<TextView>(R.id.tv_msg_content)

        if (mText.isNotEmpty() && (mText.length > 16 || mText.contains("\n"))) {
            textView.gravity = Gravity.START
        } else {
            textView.gravity = Gravity.CENTER
        }

        // 不推荐 富文本可能被修改值 导致gif动不了
        if (mText is Spanned || mText is Spannable) {
            textView.text = mText
            setSelectText()
        }
        // 推荐
        else {
            RichText.initCacheDir(context.applicationContext)
            RichText.from(mText.toString())
                .autoFix(false) // 是否自动修复宽高，默认true
                .autoPlay(true) // gif自动播放
                .done { // 在成功回调处理
                    // 演示消息列表选择文本
                    setSelectText()
                }
                .into(textView)
        }

        findViewById<View>(R.id.rl_selector).setOnClickListener { v: View? ->
            if (!SelectHelper.isPopShowing(textView)){
                dismiss()
                return@setOnClickListener
            }
            SelectHelper.resetSelect(textView)
        }
    }

    private fun setSelectText() {
        val builder = SelectBuilder(textView)
            .setCursorHandleColor(ContextCompat.getColor(context, R.color.colorAccent))
            .setCursorHandleSizeInDp(24f)
            .setSelectedColor(ContextCompat.getColor(context, R.color.colorAccentTransparent))
            .setSelectAll(false)
        SelectHelper.push(builder,object : OnSelectListenerImpl() {
            override fun onTextSelected(content: CharSequence?) {
                selectText = content
            }
        })
        textView.setOnClickListener {
            if (!SelectHelper.isPopShowing(it)){
                dismiss()
                return@setOnClickListener
            }
            SelectHelper.resetSelect(it)
        }
        textView.setOnLongClickListener {
            SelectHelper.startSelect(it)
            return@setOnLongClickListener true
        }
    }

    override fun dismiss() {
        SelectHelper.resetSelect(textView)
        super.dismiss()
    }

    /**
     * 复制
     */
    private fun copy(selectText: String?) {
        SelectHelper.resetSelect(textView)
        val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        cm.setPrimaryClip(ClipData.newPlainText(selectText, selectText))
        toast("已复制")
    }

    /**
     * 全选
     */
    private fun selectAll() {
        SelectHelper.selectAll(textView)
    }

    /**
     * 转发
     */
    private fun forward(content: String?) {
        SelectHelper.resetSelect(textView)
        toast("转发")
    }

    private fun toast(msg: String) {
        Toast.makeText(context.applicationContext, msg, Toast.LENGTH_SHORT).show()
    }
}